﻿using System;
using System.Collections.Generic;
using TaleWorlds.CampaignSystem.CharacterDevelopment;
using TaleWorlds.CampaignSystem.Extensions;
using TaleWorlds.CampaignSystem.Settlements;
using TaleWorlds.Core;
using TaleWorlds.Library;
using TaleWorlds.Localization;
using TaleWorlds.SaveSystem;

namespace TaleWorlds.CampaignSystem.Party.PartyComponents
{
	// Token: 0x020002AE RID: 686
	public class LordPartyComponent : WarPartyComponent
	{
		// Token: 0x060027CD RID: 10189 RVA: 0x000AA774 File Offset: 0x000A8974
		internal static void AutoGeneratedStaticCollectObjectsLordPartyComponent(object o, List<object> collectedObjects)
		{
			((LordPartyComponent)o).AutoGeneratedInstanceCollectObjects(collectedObjects);
		}

		// Token: 0x060027CE RID: 10190 RVA: 0x000AA782 File Offset: 0x000A8982
		protected override void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
		{
			base.AutoGeneratedInstanceCollectObjects(collectedObjects);
			collectedObjects.Add(this._leader);
			collectedObjects.Add(this.Owner);
		}

		// Token: 0x060027CF RID: 10191 RVA: 0x000AA7A3 File Offset: 0x000A89A3
		internal static object AutoGeneratedGetMemberValueOwner(object o)
		{
			return ((LordPartyComponent)o).Owner;
		}

		// Token: 0x060027D0 RID: 10192 RVA: 0x000AA7B0 File Offset: 0x000A89B0
		internal static object AutoGeneratedGetMemberValue_leader(object o)
		{
			return ((LordPartyComponent)o)._leader;
		}

		// Token: 0x060027D1 RID: 10193 RVA: 0x000AA7BD File Offset: 0x000A89BD
		internal static object AutoGeneratedGetMemberValue_wagePaymentLimit(object o)
		{
			return ((LordPartyComponent)o)._wagePaymentLimit;
		}

		// Token: 0x17000A01 RID: 2561
		// (get) Token: 0x060027D2 RID: 10194 RVA: 0x000AA7CF File Offset: 0x000A89CF
		public override Hero PartyOwner
		{
			get
			{
				return this.Owner;
			}
		}

		// Token: 0x17000A02 RID: 2562
		// (get) Token: 0x060027D3 RID: 10195 RVA: 0x000AA7D7 File Offset: 0x000A89D7
		public override TextObject Name
		{
			get
			{
				if (this._cachedName == null && this.Owner != null)
				{
					this._cachedName = this.GetPartyName();
				}
				return this._cachedName ?? new TextObject("{=!}unnamedMobileParty", null);
			}
		}

		// Token: 0x17000A03 RID: 2563
		// (get) Token: 0x060027D4 RID: 10196 RVA: 0x000AA80A File Offset: 0x000A8A0A
		public override Settlement HomeSettlement
		{
			get
			{
				return this.Owner.HomeSettlement;
			}
		}

		// Token: 0x17000A04 RID: 2564
		// (get) Token: 0x060027D5 RID: 10197 RVA: 0x000AA817 File Offset: 0x000A8A17
		// (set) Token: 0x060027D6 RID: 10198 RVA: 0x000AA81F File Offset: 0x000A8A1F
		[SaveableProperty(20)]
		public Hero Owner { get; private set; }

		// Token: 0x17000A05 RID: 2565
		// (get) Token: 0x060027D7 RID: 10199 RVA: 0x000AA828 File Offset: 0x000A8A28
		public override Hero Leader
		{
			get
			{
				return this._leader;
			}
		}

		// Token: 0x17000A06 RID: 2566
		// (get) Token: 0x060027D8 RID: 10200 RVA: 0x000AA830 File Offset: 0x000A8A30
		public override int WagePaymentLimit
		{
			get
			{
				return this._wagePaymentLimit;
			}
		}

		// Token: 0x060027D9 RID: 10201 RVA: 0x000AA838 File Offset: 0x000A8A38
		public override void SetWagePaymentLimit(int newLimit)
		{
			this._wagePaymentLimit = newLimit;
		}

		// Token: 0x060027DA RID: 10202 RVA: 0x000AA844 File Offset: 0x000A8A44
		public static MobileParty CreateLordParty(string stringId, Hero hero, Vec2 position, float spawnRadius, Settlement spawnSettlement, Hero partyLeader)
		{
			return MobileParty.CreateParty(hero.CharacterObject.StringId + "_party_1", new LordPartyComponent(hero, partyLeader), delegate(MobileParty mobileParty)
			{
				mobileParty.LordPartyComponent.InitializeLordPartyProperties(mobileParty, position, spawnRadius, spawnSettlement);
			});
		}

		// Token: 0x060027DB RID: 10203 RVA: 0x000AA89B File Offset: 0x000A8A9B
		protected internal LordPartyComponent(Hero owner, Hero leader)
		{
			this.Owner = owner;
			this._leader = leader;
		}

		// Token: 0x060027DC RID: 10204 RVA: 0x000AA8CB File Offset: 0x000A8ACB
		internal void ChangePartyOwner(Hero owner)
		{
			this.ClearCachedName();
			this.Owner = owner;
		}

		// Token: 0x060027DD RID: 10205 RVA: 0x000AA8DA File Offset: 0x000A8ADA
		public override void ChangePartyLeader(Hero newLeader)
		{
			this._leader = newLeader;
			this.ClearCachedName();
		}

		// Token: 0x060027DE RID: 10206 RVA: 0x000AA8E9 File Offset: 0x000A8AE9
		public override void ClearCachedName()
		{
			this._cachedName = null;
		}

		// Token: 0x060027DF RID: 10207 RVA: 0x000AA8F2 File Offset: 0x000A8AF2
		private TextObject GetPartyName()
		{
			TextObject textObject = GameTexts.FindText("str_lord_party_name", null);
			textObject.SetCharacterProperties("TROOP", this.Owner.CharacterObject, false);
			textObject.SetTextVariable("IS_LORDPARTY", 1);
			return textObject;
		}

		// Token: 0x060027E0 RID: 10208 RVA: 0x000AA924 File Offset: 0x000A8B24
		private void InitializeLordPartyProperties(MobileParty mobileParty, Vec2 position, float spawnRadius, Settlement spawnSettlement)
		{
			mobileParty.AddElementToMemberRoster(this.Owner.CharacterObject, 1, true);
			mobileParty.ActualClan = this.Owner.Clan;
			int troopNumberLimit = (this.Owner != Hero.MainHero && this.Owner.Clan != Clan.PlayerClan) ? ((int)MathF.Min(this.Owner.Clan.IsRebelClan ? 40f : 19f, (this.Owner.Clan.IsRebelClan ? 0.2f : 0.1f) * (float)mobileParty.Party.PartySizeLimit)) : 0;
			if (!Campaign.Current.GameStarted)
			{
				float randomFloat = MBRandom.RandomFloat;
				float num = MathF.Sqrt(MBRandom.RandomFloat);
				float num2 = 1f - randomFloat * num;
				troopNumberLimit = (int)((float)mobileParty.Party.PartySizeLimit * num2);
			}
			mobileParty.InitializeMobilePartyAroundPosition(this.Owner.Clan.DefaultPartyTemplate, position, spawnRadius, 0f, troopNumberLimit);
			mobileParty.Party.SetVisualAsDirty();
			if (spawnSettlement != null)
			{
				mobileParty.Ai.SetMoveGoToSettlement(spawnSettlement);
			}
			mobileParty.Aggressiveness = 0.9f + 0.1f * (float)this.Owner.GetTraitLevel(DefaultTraits.Valor) - 0.05f * (float)this.Owner.GetTraitLevel(DefaultTraits.Mercy);
			mobileParty.ItemRoster.Add(new ItemRosterElement(DefaultItems.Grain, MBRandom.RandomInt(15, 30), null));
			this.Owner.PassedTimeAtHomeSettlement = (float)((int)(MBRandom.RandomFloat * 100f));
			if (spawnSettlement != null)
			{
				mobileParty.Ai.SetMoveGoToSettlement(spawnSettlement);
			}
		}

		// Token: 0x04000C26 RID: 3110
		[CachedData]
		private TextObject _cachedName;

		// Token: 0x04000C28 RID: 3112
		[SaveableField(30)]
		private Hero _leader;

		// Token: 0x04000C29 RID: 3113
		[SaveableField(40)]
		private int _wagePaymentLimit = Campaign.Current.Models.PartyWageModel.MaxWage;
	}
}
